package com.woniuxy.thread;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

class Demo1 {
	public synchronized void m1() {
		System.out.println("Demo1 m1()");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized void m2() {
		System.out.println("Demo1 m2()");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	static public synchronized void m3() {
		System.out.println("Demo1 m3()");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	static public void m4() {
		System.out.println("Demo1 m4()");
		try {
			TimeUnit.SECONDS.sleep(2);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

public class SynDemo1 {
	public static void main(String[] args) {
		Class c = Demo1.class;
		Demo1 demo1 = new Demo1();
		Demo1 demo2 = new Demo1();

//		new Thread(() -> {
//			demo1.m1();
//		}).start();

//		new Thread(() -> {
//			demo1.m2();
//		}).start();

		new Thread(() -> {
//			demo1.m3();
			Demo1.m3();
		}).start();
		new Thread(() -> {
//			demo2.m4();
			Demo1.m4();
		}).start();

		List<String> data = new ArrayList<String>(); // 线程不安全
		List<String> data1 = Collections.synchronizedList(data); // 线程安全
	}

}
